﻿Imports System.ComponentModel
Imports System.Security.Cryptography
Imports System.Collections.ObjectModel

Namespace Tools

''' <summary>
''' The feature is not implemented.
''' </summary>
''' <remarks></remarks>
Public Class NotImplementedException
    Inherits Exception

End Class

''' <summary>
''' Cycle between min and max number.
''' </summary>
''' <remarks></remarks>
Public Structure Cycle

    ' Private fields
    Dim _val, _min, _max As Integer

    ' Constructor
    Public Sub New(ByVal min As Integer, ByVal max As Integer)
        _val = min : _min = min : _max = max
    End Sub

    ' Public members
    Public Property Value() As Integer
        Get
            Return _val
        End Get
        Set(ByVal value As Integer)
            ' Ensure new setting is between _min and _max.
            If value > _max Then _val = _min _
            Else If value < _min Then _val = _max _
            Else _val = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return Value.ToString
    End Function

    Public Function ToInteger() As Integer
        Return Value
    End Function

    ' Operators
    Public Shared Operator +(ByVal arg1 As Cycle, _
    ByVal arg2 As Integer) As Cycle
        arg1.Value += arg2
        Return arg1
    End Operator

    Public Shared Operator -(ByVal arg1 As Cycle, _
    ByVal arg2 As Integer) As Cycle
        arg1.Value -= arg2
        Return arg1
    End Operator

End Structure

    Public Class General

        ''' <summary>
        ''' Extracts numbers from a string and join them together
        ''' </summary>
        ''' <param name="expr">String from which extract numbers</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function ExtractNumbers(ByVal expr As String) As Integer
            Dim ret = String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(expr, "[^\d]"))
            Try
                ret = Integer.Parse(expr)
            Catch ex As Exception
                'noop
            End Try
            Return ret
        End Function

        ''' <summary>
        ''' Returns a random number between Min and Max using a brand new generator with seed set to System.DateTime.Now.Millisecond.
        ''' </summary>
        ''' <param name="Min"></param>
        ''' <param name="Max"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer

            Dim generator As New Random(System.DateTime.Now.Millisecond)

            Return generator.Next(Min, Max)

        End Function

        ''' <summary>
        ''' Returns a random string of a specified lenght.
        ''' </summary>
        ''' <param name="Lenght"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetRandom(ByVal Lenght As Integer) As String

            Dim ret As String = ""

            For i = 1 To Lenght

                ret &= Chr(GetRandom(0, 255))

            Next

            Return ret

        End Function

    End Class

End Namespace

